<?php
namespace api\toilet\controller;

use api\admin\model\BandToiletModel;
use cmf\controller\RestBaseController;
use api\admin\model\ToiletFlowModel;

class FlowController extends RestBaseController
{

    /**
     * [hikvision 海康流量]
     * @return [type] [description]
     */
    public function hikvision()
    {
        $xmlData = file_get_contents('php://input'); //PHP7接收

        if ($xmlData)
        {

            //写入日志
            cmf_log("[" . date('Y-m-d H:i:s', time()) . "]" . json_encode($xmlData) . PHP_EOL, RUNTIME_PATH.'hikvision.log');

            //解析数据
            $xml     = simplexml_load_string($xmlData);
            $xmlJson = json_encode($xml);
            $xmlArr  = json_decode($xmlJson, true);

            //获得设备id
            $deviceId = str_replace(':', '', $xmlArr['macAddress']);

            $enter = $xmlArr['peopleCounting']['enter']; //进入人数
            $exit  = $xmlArr['peopleCounting']['exit']; //离开人数
            // $all      = $xmlArr['activePostCount'];
            $BandToilet = new BandToiletModel();
            $toiletRes  = $BandToilet->getBandToiletFields(['mac' => $deviceId]);

            $index      = new IndexController();
            $ToiletFlow = new ToiletFlowModel();

            //查询是否绑定该设备
            if ($toiletRes)
            {
                //
                $RedisKey = $index->getRedisKey($toiletRes);
                $redis = new \Redis();
                $redis->connect('127.0.0.1', 6379);

                //
                cmf_log("[" . date('Y-m-d H:i:s', time()) . "]" . $RedisKey . '_today_flow' . PHP_EOL, RUNTIME_PATH.'hikvisionkey.log');


                //判断当前缓存是否被清空
                if (!$redis->get($RedisKey . '_today_flow') || !$redis->get($RedisKey . '_all_flow'))
                {

                    //判断当天缓存是否为空
                    if (!$redis->get($RedisKey . '_today_flow'))
                    {
                        $Flow['all'] = $ToiletFlow->where(['mac' => $deviceId])->sum('user_in');
                    }

                    $Flow['today'] = $ToiletFlow
                    ->where(['mac' => $deviceId])->whereTime('create_time', 'today')->value('user_in');
                    $redis->set($RedisKey . '_today_flow', (int) $Flow['today']);
                    $redis->set($RedisKey . '_all_flow', (int) $Flow['all']);
                }

                //当日流量清零
                if (date('H') >= 23)
                {
                    $redis->set($RedisKey . '_today_flow', 0);
                    $Flow['all'] = $redis->incr($RedisKey . '_all_flow', (int) $enter);
                }
                else
                {
                    $Flow['today'] = $redis->incr($RedisKey . '_today_flow', (int) $enter);
                    $Flow['all'] = $redis->incr($RedisKey . '_all_flow', (int) $enter);
                }

                // print_r($Flow);exit();

                //累计流量
                $redis->hset($RedisKey, trim($toiletRes['mac']), serialize($Flow)); //

                //当天流量插入数据库
                $toiletData = [
                    'mac' => $deviceId,
                    'user_in' => $redis->get($RedisKey . '_today_flow'),
                    'master_mac' => $toiletRes['master_mac'],
                ];
                $ToiletFlow->insertFlowData($toiletData);
                //
                $redisResult = $redis->hgetall($RedisKey);
                $redis->hset('cloud_' . $toiletRes['master_mac'], trim($RedisKey), serialize($redisResult)); //

            }

        }
    
    }

}
